package lishe.myrefreshview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

import lishe.myrefreshview.MyResyclerView.RecyclerViewRefreshListener;
import lishe.myrefreshview.MyResyclerView.RefreshRecyclerView;

public class MainActivity extends AppCompatActivity {
    RefreshRecyclerView recyclerView;
    MyAdapter myAdaoter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myAdaoter = new MyAdapter(MainActivity.this);
        recyclerView = (RefreshRecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        View view = getLayoutInflater().inflate(R.layout.item, null);
        recyclerView.addHeaderView(view);
        recyclerView.addFooterView(view);
        recyclerView.setAdapter(myAdaoter);
        recyclerView.setOnRefreshListener(new RecyclerViewRefreshListener() {
            @Override
            public void onHeaderRefresh() {
                Log.e("---","onHeaderRefresh");
            }

            @Override
            public void onFooterRefresh() {
                Log.e("---","onFooterRefresh");
            }
        });
//        recyclerView.setRefreshing();
//        timer.schedule(timerTask, 1000, 1000);
    }

    int index = 0;
    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            index++;
            if (index == 1) {
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setRefreshing();
                    }
                });
            }
            if (index == 6) {
                recyclerView.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.setCompleteRefresh();
                    }
                });
            }
            if (index == 10) {
                index = 0;
            }
        }
    };
}
